<?php
/**
 * Author：晴天 QQ：1186258278
 * Creation：2020/3/19 13:44
 * Filename：admin.application.php
 * 应用商店
 */

$title = '软件商店';
include "header.php";
?>
<div class="row">
    <div class="col-xl-12">
        <div class="card">
            <div class="card-header bg-success text-white" style="line-height: 2.5em;height: 4em">软件商店
                <button class="btn btn-sm bg-white" style="float: right;" onclick="appor.relod()">更新软件列表</button>
            </div>
            <div class="card-body" style="z-index: 1">
                <a href="admin.application.php">
                    <button class="btn btn-sm <?= (empty($_QET['type']) ? 'btn-success' : 'btn-outline-success') ?>">
                        全部
                    </button>
                </a>
                <a href="admin.application.php?type=1">
                    <button class="btn btn-sm <?= ($_QET['type'] == 1 ? 'btn-success' : 'btn-outline-success') ?>">
                        实用插件
                    </button>
                </a>
                <a href="admin.application.php?type=2">
                    <button class="btn btn-sm <?= ($_QET['type'] == 2 ? 'btn-success' : 'btn-outline-success') ?>">
                        网站模板
                    </button>
                </a>
                <a href="admin.application.php?type=3">
                    <button class="btn btn-sm <?= ($_QET['type'] == 3 ? 'btn-success' : 'btn-outline-success') ?>">已购买
                    </button>
                </a>
                <hr>
                用户等级：<span id="grade" class="font-16 text-danger">1</span>级，
                当前成长值：<span id="present" class="font-16 text-warning">1</span>，
                升级还差：<span class="font-16 text-primary" id="demand">1</span>
                ，账户余额：<span id="money" class="font-16 text-danger">0</span>元 | <a href="javascript:appor.user(2)">刷新</a>
                <table class="layui-hide" id="applist"
                       data="<?= (empty($_QET['type']) ? 0 : (int)$_QET['type']) ?>"
                       lay-filter="applist"></table>
            </div>
        </div>
    </div>
</div>
<style>
    .layui-table-tool {
        display: none
    }

    .operation > a {
        color: #20a53a !important;
        cursor: pointer;
    }
</style>
<?php include "bottom.php"; ?>

<script>
    layui.use(['form', 'table', 'upload'], function () {
        var table = layui.table;
        table.render({
            elem: '#applist'
            , url: 'ajax.php?act=AppList&type=' + $("#applist").attr('data') + '&D=1'
            , toolbar: '#applistas'
            , title: '软件列表'
            , cellMinWidth: 150
            , id: 'idTest'
            , defaultToolbar: false
            , skin: 'sm'
            , cols: [[
                {field: 'name', templet: '#name', title: '软件名称', width: 150}
                , {field: 'source', title: '开发商', width: 100}
                , {field: 'content', title: '说明', width: 200}
                , {field: 'price', templet: '#price', title: '价格', width: 100}
                , {field: 'endtime', templet: '#endtime', title: '到期时间', width: 220}
                , {field: 'identification', templet: '#identification', title: '软件标识', width: 150}
                , {field: 'grade', title: '购买限制', width: 90}
                , {field: 'status', templet: "#status", title: '状态', width: 70}
                , {field: 'operation', templet: '#operation', title: '操作'}
            ]]
            , page: true
        });
    });
</script>

<script type="text/html" id="name">
    {{# if(d.state==1){ }}
    <a style="color: #888"><img
                src="{{ d.icon }}" style="margin-bottom: 0.2em;" height="20"
                width="20"/> {{ d.name }}</a>
    {{# }else{ }}
    <a href="javascript:appor.set('{{ d.identification }}','{{ d.name }}','{{ d.id }}')" style="color: #06011a"><img
                src="{{ d.icon }}" style="margin-bottom: 0.2em;" height="20"
                width="20"/ > {{ d.name }}</a>
    {{# } }}

</script>

<script type="text/html" id="identification">
    {{# if(d.state==1){ }}
    {{ d.identification }}
    {{# }else{ }}
    <a href="javascript:appor.help('{{ d.identification }}','{{ d.name }}')">
        {{ d.identification }}
    </a>
    {{# } }}
</script>

<script type="text/html" id="price">
    {{# if(d.price==0){ }}
    免费
    {{# }else{ }}
    <span style="color:#fc6d26">￥{{d.price}}</span>
    {{# } }}
</script>

<script type="text/html" id="status">
    {{# if(d.state==1){ }}
    --
    {{# }else{ }}
    {{# if(d.status==1){ }}
    <span class="text-success layui-icon layui-icon-ok-circle" onclick="appor.state('{{ d.identification }}',2)"
          title="已开启"></span>
    {{# }else{ }}
    <span class="text-red layui-icon layui-icon-close-fill" onclick="appor.state('{{ d.identification }}',1)"
          style="color: red" title="已关闭"></span>
    {{# } }}
    {{# } }}
</script>

<script type="text/html" id="endtime">
    {{# if(d.endtime==undefined){ }}
    --
    {{# }else{ }}
    {{ d.endtime }} <a href="javascript:appor.prolong({{ d.id }},'{{ d.name }}')">续期</a>
    {{# } }}
</script>

<script type="text/html" id="operation">
    <div class="operation">
        {{# if(d.state==1){ }}
        {{# if(d.price==0){ }}
        <a href="javascript:appor.install({{ d.id }},'{{ d.name }}')">安装</a>
        {{# }else{ }}
        {{# if(d.pid==undefined){ }}
        <a href="javascript:appor.pay({{ d.id }},'{{ d.price }}','{{ d.name }}')">立即购买</a>
        {{# }else{ }}
        <a href="javascript:appor.install({{ d.id }},'{{ d.name }}')">安装</a>
        {{# } }}
        {{# } }}
        {{# }else if(d.state==2){ }}
        <a href="javascript:appor.set('{{ d.identification }}','{{ d.name }}','{{ d.id }}')">设置</a> | <a
                href="javascript:appor.unload('{{ d.identification }}','{{ d.name }}')">卸载</a>
        {{# }else if(d.state==3){ }}
        <a href="javascript:appor.update({{ d.id }})">更新</a> | <a
                href="javascript:appor.set('{{ d.identification }}','{{ d.name }}','{{ d.id }}')">设置</a> | <a
                href="javascript:appor.unload('{{ d.identification }}','{{ d.name }}')">卸载</a>
        {{# }else if(d.state==4){ }}
        <a href="javascript:appor.prolong({{ d.id }},'{{ d.name }}')">立即续期</a> | <a
                href="javascript:appor.unload('{{ d.identification }}','{{ d.name }}')">卸载</a>
        {{# }else{ }}
        <a href="javascript:appor.unload('{{ d.identification }}','{{ d.name }}')">卸载</a>
        {{# } }}
    </div>
</script>

<script>
    var appor = {
        user: function (type = 1) {
            $.ajax({
                type: "POST",
                url: "ajax.php?act=app_users",
                dataType: "json",
                success: function (data) {
                    if (data.code >= 0) {
                        $.each(data, function (key, val) {
                            $("#" + key).text(val);
                        });
                        if (type == 2) {
                            layer.msg('刷新成功', {icon: 1, time: 500});
                        }
                    } else layer.msg(data.msg);
                },
                error: function () {
                    layer.alert('用户数据获取失败！');
                }
            });
        },
        pay: function (id, price, name) {
            layer.open({
                title: '操作确认',
                content: '是否要购买：' + name + '30天使用权？<br>这将会花费您' + price + '元服务端余额,请确保余额充足！',
                btn: ['购买', '取消'],
                icon: 3,
                btn1: function (layero, index) {
                    layer.msg('正在购买中...', {icon: 16, time: 666666});
                    $.ajax({
                        type: "post",
                        url: "ajax.php?act=app_pay&id=" + id,
                        dataType: "json",
                        success: function (data) {
                            layer.closeAll();
                            if (data.code >= 0) {
                                appor.relod();
                                layer.msg(data.msg, {icon: 1});
                            } else {
                                layer.msg(data.msg, {icon: 2});
                            }
                        },
                        error: function () {
                            layer.closeAll();
                            appor.relod();
                            layer.alert('购买失败！');
                        }
                    });
                }
            });
        },
        update: function (id) {
            layer.msg('正在获取更新内容...', {icon: 16, time: 666666});
            $.ajax({
                type: "post",
                url: "ajax.php?act=app_update_log&id=" + id,
                dataType: "json",
                success: function (data) {
                    layer.closeAll();
                    if (data.code >= 0) {
                        layer.open({
                            title: 'v' + data.data.versions + ' 更新日志',
                            content: data.data.update_instructions,
                            btn: ['更新', '取消'],
                            icon: 1,
                            btn1: function (layero, index) {
                                layer.msg('正在更新中...', {icon: 16, time: 666666});
                                $.ajax({
                                    type: "post",
                                    url: "ajax.php?act=app_install&id=" + id + '&type=2',
                                    dataType: "json",
                                    success: function (data) {
                                        layer.closeAll();
                                        if (data.code >= 0) {
                                            appor.relod();
                                            layer.msg(data.msg, {icon: 1});
                                        } else {
                                            layer.msg(data.msg, {icon: 2});
                                        }
                                    },
                                    error: function () {
                                        layer.closeAll();
                                        appor.relod();
                                        layer.alert('更新失败！');
                                    }
                                });
                            }
                        });
                    } else layer.msg(data.msg, {icon: 2});
                },
                error: function () {
                    layer.closeAll();
                    layer.alert('获取失败！');
                }
            });
        },
        prolong: function (id, name) {
            layer.open({
                title: '操作确认',
                content: '是否要为' + name + '延长到期时间?',
                btn: ['确认', '取消'],
                icon: 3,
                btn1: function (layero, index) {
                    layer.msg('正在执行中...', {icon: 16, time: 666666});
                    $.ajax({
                        type: "post",
                        url: "ajax.php?act=app_prolong&id=" + id,
                        dataType: "json",
                        success: function (data) {
                            layer.closeAll();
                            if (data.code >= 0) {
                                appor.relod();
                                layer.msg(data.msg, {icon: 1});
                            } else {
                                layer.msg(data.msg, {icon: 2});
                            }
                        },
                        error: function () {
                            layer.closeAll();
                            appor.relod();
                            layer.alert('操作失败！');
                        }
                    });
                }
            });
        },
        set: function (id, name, t) {
            if (document.body.clientWidth > 750) {
                area = ['90%', '90%'];
            } else area = ['96%', '96%'];
            layer.open({
                type: 2,
                skin: 'layui-layer-rim',
                area: area,
                title: name + '操作面板',
                content: 'ajax.php?act=app_view&id=' + id + '&path=index&t=' + t,
            });
        },
        help: function (id, name) {
            if (document.body.clientWidth > 750) {
                area = ['90%', '90%'];
            } else area = ['96%', '96%'];
            layer.open({
                type: 2,
                skin: 'layui-layer-rim',
                area: area,
                title: name + '帮助文档',
                content: 'ajax.php?act=app_help&id=' + id,
            });
        },
        unload: function (id, name) {
            layer.open({
                title: '操作确认',
                content: '是否要卸载' + name + '？<br>卸载只可卸载配置文件，视图文件，和控制器文件等!',
                btn: ['确认', '取消'],
                icon: 3,
                btn1: function (layero, index) {
                    layer.msg('正在卸载中...', {icon: 16, time: 666666});
                    $.ajax({
                        type: "post",
                        url: "ajax.php?act=app_unload&id=" + id,
                        dataType: "json",
                        success: function (data) {
                            layer.closeAll();
                            if (data.code >= 0) {
                                appor.relod();
                                layer.msg(data.msg, {icon: 1});
                            } else {
                                layer.msg(data.msg, {icon: 2});
                            }
                        },
                        error: function () {
                            layer.closeAll();
                            appor.relod();
                            layer.alert('卸载失败！');
                        }
                    });
                }
            });
        },
        install: function (id, name) {
            layer.open({
                title: '操作确认',
                content: '是否要安装' + name + '？',
                btn: ['安装', '取消'],
                icon: 3,
                btn1: function (layero, index) {
                    layer.msg('正在安装中...', {icon: 16, time: 666666});
                    $.ajax({
                        type: "post",
                        url: "ajax.php?act=app_install&id=" + id + '&type=1',
                        dataType: "json",
                        success: function (data) {
                            layer.closeAll();
                            if (data.code >= 0) {
                                appor.relod();
                                layer.msg(data.msg, {icon: 1});
                            } else {
                                layer.msg(data.msg, {icon: 2});
                            }
                        },
                        error: function () {
                            layer.closeAll();
                            appor.relod();
                            layer.alert('安装失败！');
                        }
                    });
                }
            });
        },
        state: function (id, type) {
            layer.msg('状态切换中...', {icon: 16, time: 666666});
            $.ajax({
                type: "post",
                url: "ajax.php?act=app_state_set&id=" + id + '&type=' + type,
                dataType: "json",
                success: function (data) {
                    layer.closeAll();
                    if (data.code >= 0) {
                        appor.relod();
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                },
                error: function () {
                    layer.closeAll();
                    appor.relod();
                    layer.alert('切换失败！');
                }
            });
        },
        relod() {
            var table = layui.table;
            table.reload('idTest', {
                url: 'ajax.php?act=AppList&type=' + $("#applist").attr('data') + '&D=2',
            });
            appor.user();
        }
    };
    appor.user();
</script>
